package net.akkz.beanFactory;

import java.io.InputStream;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.akkz.model.Action;
import net.akkz.model.Result;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

public class ActionFactoryWithPermission
{
	private Map<String, Action> actions;
	private Map<String, Object> beans;
	private Map<String, Result> defaultResult;

	public void init()
	{
		actions = new HashMap<String, Action>();
		
		try
		{
			InputStream is = this.getClass().getClassLoader().getResourceAsStream("action.xml");
			SAXReader reader = new SAXReader();
			Document doc = reader.read(is);
			Element root = doc.getRootElement();
			
			//------初始参数------
			Iterator<Element> i = root.elementIterator("action");
			while (i.hasNext())
			{
				Element action = (Element) i.next();
				String id = action.attributeValue("id");
				Object clazz = beans.get(action.attributeValue("ref"));
				String permission = action.attributeValue("permission");
				
				Action a = new Action();
				a.setId(id);
				a.setClazz(clazz);
				a.setPermission(permission);
				a.setResults(new HashMap<String, Result>());
				
				Iterator b = action.elementIterator("result");
				while(b.hasNext())
				{
					Element result = (Element)b.next();
					String name = result.attributeValue("name");
					String view = result.getText();
					String redirectWay = result.attributeValue("redirectWay");
					
					Result r = new Result();
					r.setName(name);
					r.setView(view);
					r.setRedirectWay(redirectWay);
					
					a.addResult(name, r);
				}
			}
			
			i = root.elementIterator("result");
			while (i.hasNext())
			{
				Element result = (Element) i.next();
				String name = result.attributeValue("name");
				String redirectWay = result.attributeValue("redirectWay");
				String view = result.getTextTrim();
				
				Result r = new Result();
				r.setName(name);
				r.setRedirectWay(redirectWay);
				r.setView(view);
				
				defaultResult.put(name, r);
			}
		}
		catch (DocumentException e)
		{
			e.printStackTrace();
		}
		catch (NullPointerException e)
		{
			e.printStackTrace();
		}
	}
	
	public Result excute(String action, String method, HttpServletRequest request, HttpServletResponse response)
	{
		try
		{
			Action a = actions.get(action);
			
			String permission = (String) request.getSession().getAttribute("permission");
			if(permission != null)
			{
				String cp = a.getPermission();
				boolean flag = false;
				
				for(String per : permission.split(" "))
				{
					if(cp.contains(per))
					{
						flag = true;
						break;
					}
				}
				
				if(flag)
				{
					return defaultResult.get("page503");
				}
			}
			
			Object o = a.getClazz();
			
			Method m = o.getClass().getMethod(method, HttpServletRequest.class, HttpServletResponse.class);
			
			return (Result) m.invoke(o, request, response);
		}
		catch (NullPointerException e)
		{
			e.printStackTrace();
			
			return defaultResult.get("page404");
		}
		catch (NoSuchMethodException | SecurityException | IllegalAccessException | IllegalArgumentException | InvocationTargetException e)
		{
			e.printStackTrace();
			
			return defaultResult.get("page503");
		}
	}

	@SuppressWarnings("unchecked")
	public Action getAction(String name)
	{
		return actions.get(name);
	}

	public void setBeans(Map<String, Object> beans)
	{
		this.beans = beans;
	}
}
